package com.wz.utils.concurrency.threadpool;

import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author : Wang Zhen.
 * @Date : Created in 12:34 2022/11/16
 * @Description :
 * @Modified By   :
 * @Version :
 */
public class ScheduledDemo {
    public static void main(String[] args) {
        ScheduledThreadPoolExecutor pool = (ScheduledThreadPoolExecutor)Executors.newScheduledThreadPool(5);
        Task task = new Task("任务");
        System.out.println("Created: " + task.getName());
//        pool.schedule(task, 2, TimeUnit.SECONDS);
        pool.scheduleWithFixedDelay(task, 0, 2, TimeUnit.SECONDS);
//        pool.scheduleAtFixedRate(task, 0, 1, TimeUnit.SECONDS)
    }
}

class Task implements Runnable {
    private String name;

    public Task(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        System.out.println("执行" + name + ", 当前时间：" + new Date().getTime());
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public String getName() {
        return name;
    }
}